<?php
	require("libraries/student.php");
	class Guides extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->Model("model_guide");
			$this->load->library("string");	
		}
		public function index(){
			$config['base_url'] = base_url().'admin/guides/index/';
			$config['total_rows'] = $this->model_guide->count_all();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$config['first_link'] = "First";
			$config['last_link'] = "Last";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['title']="Travel guides";
			$data['act'] = 0;
			$data['data']['info']= $this->model_guide->listall($config['per_page'],$start);
			$data['template'] = "guides/list_guide";
			$this->load->view("layout",$data);
		}
		public function add(){
			$this->load->helper(array("form","date"));
			$this->load->library("form_validation");
			$data['error'] = "";
			$data['data'] = "";
			$data['act'] = 0;
			$data['title'] = "Travel guides";
			$data['template'] = "guides/add_guide";
			//$data['listcago'] = $this->model_guide->listcago();
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("guide_title","Guide title","min_length[5]");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('guide_title'));
					  $url = str_replace(' ', '-',strtolower($this->string->replace($title)));
					  $db = array(
							"guide_title" => $this->input->post("guide_title"),
							"guide_rewrite" => $url,
							"guide_info" => $this->input->post("guide_info"),
							"guide_full" => $this->input->post("guide_full"),
							"guide_date" => date("m/d/Y - H:i:s")
						);
					if($_FILES['img']['name'] != NULL){
						$config['upload_path'] = './uploads/news';
						$config['allowed_types'] = 'gif|jpg|jpeg|png';
						$config['max_size']	= '2000';
						$config['max_width']  = '1400';
						$config['max_height']  = '1400';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("img")){
							$data = array('error' => $this->upload->display_errors());
							$data['data'] = "";
							$data['title'] = "Travel guides";
							$data['act'] = 0;
							$data['template'] = "guides/add_guide";
							//$data['listcago'] = $this->model_guide->listcago();
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['guide_images'] = $data['file_name'];
							$this->createThumbnail("uploads/news",$db['guide_images'],180,265);
						}
					 }
					 $this->model_guide->add_guide($db);
					 redirect(base_url()."admin/guides/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$this->load->helper(array("form","date"));
			$this->load->library("form_validation");
			$data['error'] = "";
			$data['title'] = "Travel guides";
			$data['template'] = "guides/edit_guide";
			$data['data'] = "";
			$data['act'] = 0;
			$data['get'] = $this->model_guide->get_guidedata($id);
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("guide_title","Guide title","min_length[5]");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					  $title = trim($this->input->post('guide_title'));
					  $url = str_replace(' ', '-',strtolower($this->string->replace($title)));
					  $db = array(
							"guide_title" => $this->input->post("guide_title"),
							"guide_rewrite" => $url,		
							"guide_info" => $this->input->post("guide_info"),
							"guide_full" => $this->input->post("guide_full"),
							"guide_date" => date("m/d/Y - H:i:s")
						);
					if($_FILES['img']['name'] != NULL){
						$config['upload_path'] = './uploads/news';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '2000';
						$config['max_width']  = '1400';
						$config['max_height']  = '1400';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("img")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] = "Travel guides";
							$data['template'] = "guides/edit_guide";
							$data['data'] = "";
							$data['act'] = 0;
							$data['get'] = $this->model_guide->get_guidedata($id);
							$data['stt'] = $data['get']['cago_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['guide_images'] = $data['file_name'];
							$this->createThumbnail("uploads/news",$db['guide_images'],180,265);
						}
					 }
					 $this->model_guide->update($db,$id);
					 redirect(base_url()."admin/guides/index");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function search(){
			$title = $this->input->post("val");
			if($title == NULL){
				echo "Bạn phải nhập tiêu đề tin!";
			}else{
				$data  = $this->model_guide->search_ajax($title);
				if($data == NULL){
					echo "Không tìm thấy tin tức nào!";
				}else{
					echo '<table cellpadding="0" cellspacing="0" width="905">
									<tbody><tr>
									  <th width="23">Stt.</th>
									  <th width="300">Tiêu đề tin</th>
									  <th width="136">Tác giả</th>
									  <th width="132">Ngày đăng</th>
									  <th width="64">Actions</th>
									</tr>';
					$stt = 0;
					foreach($data as $items){
						$stt++;
						echo "<tr>";	
						echo "<td>$stt</td>";
						echo "<td>".$items['guide_title']."</td>";
						echo "<td>".$items['guide_author']."</td>";
						echo "<td>".$items['guide_date']."</td>";
						echo "<td>";
							echo "<div class='actions_menu'><ul>";
								echo "<li><a href='".base_url()."admin/guides/update/$items[guide_id]/' class='edit'>Edit</a></li>";
								echo "<li><a href='".base_url()."admin/guides/del/$items[guide_id]/' class='delete' onclick='return check()'>Del</a></li>";
							echo "</ul></div>";
						echo "</td>";

						echo "</tr>";
					}
					echo "</tbody></table>";
				}
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_guide->get_guidedata($id);
			@unlink("uploads/guides/".$data['guide_images']);
			$this->model_guide->del_guide($id);
			redirect(base_url()."admin/news");
		}
		public function createThumbnail($parth,$fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = $parth.'/'.$fileName;
			$config['new_image'] = $parth.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}